package com.tianyang.yukuaifuzs.business.main.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseTitleActivity;
import com.tianyang.yukuaifuzs.business.main.contract.MainConfig;
import com.tianyang.yukuaifuzs.business.main.contract.MyCodePlateContract;
import com.tianyang.yukuaifuzs.business.main.presenter.MyCodePlatePresenter;
import com.tianyang.yukuaifuzs.common.utils.CommonViewUtils;
import com.tianyang.yukuaifuzs.utils.DownloadImgUtils;
import com.tianyang.yukuaifuzs.utils.ToastUtil;
import com.tianyang.yukuaifuzs.utils.permissions.PermissionsConstant;
import com.tianyang.yukuaifuzs.utils.permissions.PermissionsMgr;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：我的码牌
 * Created by xyj on 2024/3/11
 */
public class MyCodePlateActivity extends BaseTitleActivity implements MyCodePlateContract.View {

    private String permission = PermissionsConstant.WRITE_STORAGE;

    @BindView(R.id.mpSdv)
    SimpleDraweeView mpSdv;
    @BindView(R.id.bottomLl)
    LinearLayout bottomLl;

    private MyCodePlateContract.Presenter mPresenter;

    private String picUrl, userId;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_my_code_plate;
    }

    @Override
    protected void initView() {
        super.initView();
        if (getIntent().hasExtra(MainConfig.INTENT_USER_ID)) {
            userId = getIntent().getStringExtra(MainConfig.INTENT_USER_ID);
        }
        mPresenter = new MyCodePlatePresenter(this, userId);
        String title = getIntent().getStringExtra(MainConfig.INTENT_TITLE);
        commonTitle.setTitle(title);
        String imageUrl = getIntent().getStringExtra(MainConfig.INTENT_IMAGE_URL);
        if (!TextUtils.isEmpty(imageUrl))
            showCompositePosterView(imageUrl);
//        UserCenterOutput output = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
//        if (output != null) {
//            if (!TextUtils.isEmpty(output.getCodePlateUrl())) {
//                showCompositePosterView(output.getCodePlateUrl());
//            } else {
//                mPresenter.getCompositePoster(output.getMchName(), output.getMemberId(), output.getSn());
//            }
//        }
    }

    @OnClick({R.id.downloadTv, R.id.shareTv})
    public void onClick(View view) {
        if (!isRespondEvent() || getContext() == null) return;
        switch (view.getId()) {
            case R.id.downloadTv:
                if (!PermissionsMgr.getInstance().hasPermission(mContext, permission)) {
                    requestPermissions(permission);
                    return;
                }
                downloadClick();
                break;
            case R.id.shareTv:
                if (TextUtils.isEmpty(picUrl)) {
                    ToastUtil.show("图片地址不存在！");
                    return;
                }
                CommonViewUtils.shareToWeChat(mContext, picUrl);
                break;
        }
    }

    private void downloadClick() {
        if (!TextUtils.isEmpty(picUrl))
            DownloadImgUtils.donwloadImg(mContext, picUrl);
        else
            ToastUtil.show("图片下载失败！");
    }

    @Override
    public void onPermissionGranted(List<String> permissions) {
        if (permissions != null && permissions.contains(permission)) {
            downloadClick();
        }
    }

    @Override
    public void showCompositePosterView(String url) {
        if (!TextUtils.isEmpty(url)) {
            picUrl = url;
            Glide.with(mContext)
                    .asBitmap()
                    .load(url)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
                            DisplayMetrics metric = new DisplayMetrics();
                            getWindowManager().getDefaultDisplay().getMetrics(metric);
                            int pxWidth = metric.widthPixels;
                            int pxHeight = pxWidth * bitmap.getHeight() / bitmap.getWidth();
                            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mpSdv.getLayoutParams();
                            params.width = pxWidth;
                            params.height = pxHeight;
                            mpSdv.setLayoutParams(params);
                            mpSdv.setImageURI(Uri.parse(url));
                            bottomLl.setVisibility(View.VISIBLE);
                        }
                    });
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MyCodePlateContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
